/**
 * 
 */
package info.niwota.frames;

import java.io.File;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

/**
 * @author qiangli
 * 
 */
public final class Preferences {

	private static final String BP_SCALED = "bp.scaled";

	private static final String BP_SCALE = "bp.scale";

	private static final String BP_SCROLL_XY = "bp.scroll.xy";

	private static final String BP_SEND_BODY = "settings.send.body";

	private static final String BP_SEND_EMAILS = "settings.send.email";

	private static final String BP_SEND_SUBJECT = "settings.send.subject";

	private static final String BP_STATUSBAR_TOGGLE = "settings.statusbar.toggle";
	
	private static final String BP_FILMSTRIP_TOGGLE = "settings.filmstrip.toggle";

	private static final String BP_FRAME_URI = "bp.frame.uri"; // file uri

	private static final String EMAIL_SEP = ",";

	private static final String SEP = "\n";
	
	//bg image
	public static final String BG_SELECT = "bp.bg.select";
	private static final String BG_PICK_PATH = "bp.bg.pick.path";
	//must match values compose_bg_select_values in arrays.xml
	public static final int BG_SELECT_WALLPAPER = 0;
	public static final int BG_SELECT_PICK = 1;
	public static final int BG_SELECT_SAMPLE_ANDROID = 2;
	public static final int BG_SELECT_SAMPLE_BLUESKEY = 3;
	public static final int BG_SELECT_SAMPLE_GOODMORNING = 4;
	public static final int BG_SELECT_NO_BACKGROUND = 5;
	public static final int BG_SELECT_GONE_WILD = 6;
	

	public static String getBody(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String defval = ctx.getString(R.string.msg_body_default);
			String s = p.getString(BP_SEND_BODY, defval);
			return s;
		} catch (Exception e) {
		}
		return null;
	}

	public static String[] getEmails(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SEND_EMAILS, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(EMAIL_SEP);
				return a;
			}
		} catch (Exception e) {
		}
		return null;
	}

	public static float getScale(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SCALE, null);
			if (!TextUtils.isEmpty(s)) {
				return Float.parseFloat(s);
			}
		} catch (Exception e) {
		}
		return 1.0f;
	}

	public static float[] getScrollXY(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String s = p.getString(BP_SCROLL_XY, null);
			if (!TextUtils.isEmpty(s)) {
				String[] a = s.split(SEP);
				float[] v = new float[18];
				for (int i = 0; i < v.length; i++) {
					v[i] = Float.parseFloat(a[i]);
				}
				return v;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String getSubject(Context ctx) {
		try {
			final SharedPreferences p = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			String defval = ctx.getString(R.string.msg_subject_default);
			String s = p.getString(BP_SEND_SUBJECT, defval);
			return s;
		} catch (Exception e) {
		}
		return null;
	}

	public static File getFrameFile(Context ctx) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		try {
			String s = p.getString(BP_FRAME_URI, null);
			if (!TextUtils.isEmpty(s)) {
				return new File(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void putScale(Context ctx, float scale) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putString(BP_SCALE, scale + "").commit();

	}

	public static void putScaled(Context ctx, boolean scaled) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putBoolean(BP_SCALED, scaled).commit();
	}

	public static void putScrollXY(Context ctx, float[] values) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		StringBuilder sb = new StringBuilder();
		if (values != null) {
			for (int i = 0; i < values.length; i++) {
				sb.append(values[i]);
				sb.append(SEP);
			}
		}
		p.edit().putString(BP_SCROLL_XY, sb.toString()).commit();
	}

	synchronized public static void putFrameFile(Context ctx, File file) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		Editor e = p.edit();
		e.putString(BP_FRAME_URI, file.getPath()).commit();
	}

	public static boolean isScaled(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(BP_SCALED, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static boolean showFilmstrip(Context ctx) {
		final boolean defvalue = true;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(BP_FILMSTRIP_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static void putShowFilmstrip(Context ctx, boolean show) {
		final SharedPreferences p = PreferenceManager
				.getDefaultSharedPreferences(ctx);
		p.edit().putBoolean(BP_FILMSTRIP_TOGGLE, show).commit();
	}
	
	public static boolean enableStatusbar(Context ctx) {
		final boolean defvalue = false;
		try {
			final SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(ctx);
			return pref.getBoolean(BP_STATUSBAR_TOGGLE, defvalue);
		} catch (Exception e) {
		}
		return defvalue;
	}

	public static String getBackgroundImagePath(Context context, String def) {
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			return pref.getString(BG_PICK_PATH, def);
		} catch (Exception e) {
		}
		return def;
	}

	public static void putBackgroundImagePath(Context context, String path) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		pref.edit().putString(BG_PICK_PATH, path).commit();
	}
	
	public static int getBackgroundSelection(Context context, int def) {
		try {
			final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
			return pref.getInt(BG_SELECT, def);
		} catch (Exception e) {
		}
		return def;
	}
	
	public static void putBackgroundSelection(Context context, int sel) {
		final SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
		pref.edit().putInt(BG_SELECT, sel).commit();
	}

	public static int getBackgroundSelectedResource(int sel, int defval) {
		switch(sel) {
		case BG_SELECT_SAMPLE_ANDROID:
			return R.drawable.bg_android;
		case BG_SELECT_SAMPLE_BLUESKEY:
			return R.drawable.bg_bluishsky;
		case BG_SELECT_SAMPLE_GOODMORNING:
			return R.drawable.bg_goodmorning;
		case BG_SELECT_NO_BACKGROUND:
			return R.drawable.bg_black;
		default:
			return defval;
		}
	}
	
	private Preferences() {
	}
}
